import {formatDate} from "src/hrpub/common/utils/utils";
import {getFillStatus, getSignStatus} from "../../../actions/commonFun";

export default class MainAction {
    constructor(comp) {
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    closeModal = () => {
        this.dispatch({
            type: 'myLeave/update',
            payload: {
                batchModalVisible: false
            }
        });
        const {editTable} = this.comp.props
        editTable.setTableData('batch_attend_list', {rows: []});
    }

    confirm = () => {
        let formCode = 'attendance_card'
        const {dispatch, myLeave, form, editTable} = this.comp.props
        const {
            setFormStatus,
            setAllFormValue,
            EmptyAllFormValue,
            getFormItemsValue,
            setItemsVisible,
            setFormItemsDisabled,
            setFormItemsRequired
        } = form
        const {
            setColEditableByKey
        } = editTable;
        EmptyAllFormValue(formCode)
        let cardDefaultVisiable = {
            fill_date: false,
            original_sign_status: false,
            original_sign_time: false,
            fill_time: false
        }
        let cardDefaultDisabled = {}
        let cardDefaultRequired = {
            fill_type_id: true
        }
        setItemsVisible(formCode, cardDefaultVisiable);
        setFormItemsDisabled(formCode, cardDefaultDisabled);
        setFormItemsRequired(formCode, cardDefaultRequired);
        setColEditableByKey('batch_attend_edit_list', 'fillDate', true)
        cardDefaultVisiable = null
        cardDefaultDisabled = null
        const attendList = editTable.getCheckedRows('batch_attend_list');
        this.dispatch({
            type: 'myLeave/update',
            payload: {
                showMode: 'card',
                isEdit: true,
                batchModalVisible: false,
                headerTitle: myLeave.subTitle
            }
        })
        dispatch({
            type: 'myLeave/AddAction',
            payload: {
                key: myLeave.activeTab,
                operat: 0
            }
        }).then(res => {
            setAllFormValue({[formCode]: res.data[formCode]});
            let editList = [];
            attendList.forEach(item => {
                if(item.data.values.markTimeT){
                    item.data.values.fillDate = {
                        value: formatDate(item.data.values.markTimeT.value)
                    }
                }
                if(item.data.values.markTime){
                    item.data.values.fillTime = item.data.values.markTime
                }
                editList.push(item.data)
            })
            editTable.setTableData('batch_attend_edit_list', {rows: editList});
            let billId = getFormItemsValue(formCode, 'pk_' + myLeave.activeTab).value;
            let approvestatus = getFormItemsValue(formCode, 'approvestatus').value;
            dispatch({
                type: 'myLeave/update',
                payload: {
                    billId,
                    approvestatus
                }
            })
        })
        setFormStatus(formCode, 'add')
        editTable.setStatus('batch_attend_edit_list', 'edit')
        editTable.setTableData('batch_attend_list', {rows: []});
    }

    modalEntered = () => {
        const {props} = this.comp;
        const {myLeave} = props;
        if (myLeave.beginValue) {
            this.monthChange(myLeave.beginValue.substring(0, 7))
        }
    }

    monthChange = (val) => {
        this.comp.setState({
            monthValue: val
        })
        if (!val) {
            return
        }
        const {editTable, myLeave: {json = {}}} = this.comp.props;
        const [year, month] = val.split('-')
        this.dispatch({
            type: 'myLeave/QueryAbnormalAttendanceList',
            payload: {
                year,
                month
            }
        }).then(res => {
            let batch_attend_list = {
                areacode: 'batch_attend_list',
                rows: []
            }
            let indexArr = []
            if (res.data) {
                res.data.forEach((item, index) => {
                    let values = {
                        fillDate: {
                            value: item['signTime'] ?
                                formatDate(item['signTime']) : formatDate(item['markTimeT'])
                        },
                        fillTime: {
                            value: item['signTimeT'] || formatDate(item['markTimeT'], ':', 'hh-mm')
                        },
                        originalSignTime: {
                            value: item['signTime']
                        },
                        listOrder: {
                            value: item['order']
                        },
                        isNeedFill: {
                            value: item['isNeedFill']
                        },
                        signState: {
                            value: item['signState'],
                            display: getSignStatus(item['signState'], json)
                        },
                        originalSignStatus: {
                            value: item['originalSignState']
                        },
                        fillStatus: {
                            value: item['fillStatus'],
                            display: getFillStatus(item['fillStatus'], json)
                        },
                        fill_reason: {
                            value: item['fill_reason']
                        },
                        markTimeT: {
                            value: item['markTimeT']
                        },
                        markTime: {
                            value: item['markTime']
                        }
                    };
                    batch_attend_list.rows.push({
                        status: '0',
                        values
                    })
                    if (item['isNeedFill'] === '0') {
                        indexArr.push(index)
                    }
                })
            }
            editTable.setTableData('batch_attend_list', batch_attend_list);
            editTable.setCheckboxDisabled('batch_attend_list', indexArr, false);
        })
    }
}